var dedupe = require('dedupe')

var a = [1, 2, 2, 3]
var b = dedupe(a)
console.log(b)
console.log(arrayDedupe(a))


var aa = [{a: 2}, {a: 1}, {a: 1}, {a: 1}]
var bb = dedupe(aa)
console.log(bb)
console.log(arrayDedupe(aa))
//result: [{a: 2}, {a: 1}]

var aaa = [{a: 2, b: 1}, {a: 1, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]
var bbb = dedupe(aaa, value => value.a)
console.log(bbb)
console.log( arrayDedupe(aaa, value => value.a))




/**
 * 剔除数组中重复的数据
 * @param  {[type]} arr    [description]
 * @param  {[type]} hasher [description]
 * @return {[type]}        [description]
 */
function arrayDedupe(arr, hasher) {
	hasher = hasher || JSON.stringify

	var one, hashed;
	var lookup = {}, clone = []
	for (var i = 0; i < arr.length; i++) {
		one = arr[i]
		hashed = hasher(one)
		if(!lookup[hashed]) {
			lookup[hashed] = true
			clone.push(one)
		}
	}

	return clone
}


/**
 * [arrayDedupe description]
 * @param  {[type]} arr    [description]
 * @param  {[type]} hasher [description]
 * @return {[type]}        [description]
 */
function arrayDedupeV2(arr, hasher) {
	if(!hasher) {
		hasher = function(item) {
			return `${typeof item}_${JSON.stringify(item)}_${item}`
		}
	}

	var one, result = []
	for (var i = 0; i < arr.length; i++) {
		one = arr[i]
		for(var j = 0; j < result.length; j++) {
			if(hasher(one) === hasher(result[j])) {
				break
			}
		}

		// 没有重复值
		if(j === result.length) {
			result.push(one)
		}
	}
	return result 
}